package com.asuka.service.impl;

import com.asuka.constant.StatusConstant;
import com.asuka.entity.*;
import com.asuka.mapper.*;
import com.asuka.service.WorkspaceService;
import com.asuka.vo.BusinessVO;
import com.asuka.vo.OverviewOrdersVO;
import com.asuka.vo.OverviewVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private WorkspaceMapper workspaceMapper;


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;


    @Override
    @Transient
    public BusinessVO businessData() {

        Integer newUsers = userMapper.newUsersNumber(LocalDate.now());

        //营业额 有效订单数量
        WorkspaceOrder wo = orderMapper.selectNowAmount(LocalDate.now(),Orders.COMPLETED);
        if(wo.getCount() == null){
            wo.setCount(0);
        }
        if(wo.getAmount() == null){
            wo.setAmount(BigDecimal.ZERO);
        }



        Integer number = orderMapper.nowAllOrder(LocalDate.now());

        //订单完成率 营业额/总订单
        Double orderCompletionRate = number.equals(0)? 0.0 : wo.getCount().doubleValue() / number.doubleValue();

        BigDecimal unitPrice = wo.getCount() ==0? BigDecimal.ZERO : wo.getAmount().divide(BigDecimal.valueOf(wo.getCount()));

        return BusinessVO.builder()
                .newUsers(newUsers)
                .turnover(wo.getAmount())
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompletionRate)
                .validOrderCount(wo.getCount())
                .build();
    }


    @Override
    public OverviewVO overviewSetmeals() {

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(StatusConstant.DISABLE);

        Integer disable = setmealMapper.selectSetmeal(setmeal).size();

        setmeal.setStatus(StatusConstant.ENABLE);

        Integer enable = setmealMapper.selectSetmeal(setmeal).size();


        return OverviewVO.builder()
                .discontinued(disable)
                .sold(enable)
                .build();
    }


    @Override
    public OverviewVO overviewDishes() {
        Dish dish = new Dish();

        dish.setStatus(StatusConstant.ENABLE);

        Integer enable = dishMapper.selectList(dish).size();

        dish.setStatus(StatusConstant.DISABLE);
        Integer disable = dishMapper.selectList(dish).size();


        return OverviewVO.builder()
                .discontinued(disable)
                .sold(enable)
                .build();
    }


    @Override
    public OverviewOrdersVO overviewOrders() {

        List<OrderStatus> oslist = orderMapper.selectAllStatus();

        HashMap<Integer,Integer> hashMap = new HashMap<>();
        for (OrderStatus orderStatus : oslist) {
            hashMap.put(orderStatus.getStatus(),orderStatus.getCount());
        }

        Integer cancelCount = hashMap.getOrDefault(Orders.CANCELLED,0);
        Integer compleCount = hashMap.getOrDefault(Orders.COMPLETED,0);
        Integer deliverCount = hashMap.getOrDefault(Orders.DELIVERY_IN_PROGRESS,0);
        Integer waitCount = hashMap.getOrDefault(Orders.TO_BE_CONFIRMED,0);

        Integer count = orderMapper.selectAllCount();

        return OverviewOrdersVO.builder()
                .cancelledOrders(cancelCount)
                .completedOrders(compleCount)
                .deliveredOrders(deliverCount)
                .waitingOrders(waitCount)
                .allOrders(count)
                .build();
    }
}
